package com.lyh.hkcloud.util.base;

import android.content.Context;
import android.widget.Toast;
import com.lyh.hkcloud.module.m1300084.IndexActivity;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.*;

public class JsonUtil {

    public static JSONObject put(String key, Object value) {
        JSONObject object = new JSONObject();
        return put(object, key, value);
    }

    public static JSONObject put(JSONObject object, String key, Object value) {
        if (object == null) {
            object = new JSONObject();
        }
        try {
            object.put(key, value);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return object;
    }

    public static void put(List<Map<String, Object>> list, String key, Object value) {
        if (list == null || list.isEmpty()) {
            return;
        }
        for (Map<String, Object> map : list) {
            map.put(key, value);
        }
    }

    public static void put(JSONArray array, String key, Object value) {
        if (array == null || array.length() == 0) {
            return;
        }
        int length = array.length();
        for (int i = 0; i < length; i++) {
            put(getJSONObject(array, i), key, value);
        }
    }

    public static String getString(JSONObject object, String name) {
        return getString(object, name, "");
    }

    public static String getString(JSONObject object, String name, String defaultValue) {
        if (object == null || object.isNull(name)) {
            return defaultValue;
        }
        return object.optString(name);
    }


    public static int getInt(JSONObject object, String name, int defaultValue) {
        if (object == null || object.isNull(name)) {
            return defaultValue;
        }
        return object.optInt(name);
    }

    public static double getDouble(JSONObject object, String name, double defaultValue) {
        if (object == null || object.isNull(name)) {
            return defaultValue;
        }
        return object.optDouble(name);
    }

    public static boolean getDouble(JSONObject object, String name, boolean defaultValue) {
        if (object == null || object.isNull(name)) {
            return defaultValue;
        }
        return object.optBoolean(name);
    }

    public static String getString(JSONArray array, int index, String name) {
        JSONObject object = getJSONObject(array, index);
        return getString(object, name);
    }

    public static JSONObject getJSONObject(JSONObject object, String name) {
        return getJSONObject(object, name, getBlankJSONObject());
    }

    public static JSONObject getJSONObject(JSONObject object, String name, int index) {
        JSONArray array = getJSONArray(object, name);
        return getJSONObject(array, index);
    }

    public static JSONObject getJSONObject(JSONArray array, int index) {
        if (array != null && array.length() >= (index + 1)) {
            return array.optJSONObject(index);
        }
        return getBlankJSONObject();
    }

    public static JSONObject getJSONObject(JSONObject object, String name, JSONObject defaultValue) {
        if (object == null || object.isNull(name)) {
            return defaultValue;
        }
        return object.optJSONObject(name);
    }

    public static JSONArray getJSONArray(JSONObject object, String name) {
        return getJSONArray(object, name, getBlankJSONArray());
    }

    public static JSONArray getJSONArray(JSONObject object, String name, JSONArray defaultValue) {
        if (object == null || object.isNull(name)) {
            return defaultValue;
        }
        return object.optJSONArray(name);
    }

    public static JSONObject getBlankJSONObject() {
        return new JSONObject();
    }

    public static JSONArray getBlankJSONArray() {
        return new JSONArray();
    }


    public static JSONArray toJSONArray(List<?> list) {
        JSONArray array = new JSONArray();
        for (Object o : list) {
            if (o instanceof Map) {
                array.put(toJSONObject((Map) o));
            } else if (o instanceof String || o instanceof Integer || o instanceof Float || o instanceof Double) {
                array.put(o);
            }
        }
        return array;
    }

    public static JSONObject toJSONObject(Map<String, Object> map) {
        if (map == null) {
            return getBlankJSONObject();
        }
        Set<String> set = map.keySet();
        JSONObject object = new JSONObject();
        try {
            for (String item : set) {
                Object obj = map.get(item);
                if (obj instanceof Map) {
                    object.put(item, toJSONObject((Map<String, Object>) obj));
                } else if (obj instanceof List) {
                    object.put(item, toJSONArray((List<?>) obj));
                } else {
                    object.put(item, map.get(item));
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return object;
    }

    public static List<Map<String, Object>> getList(JSONObject object, String name) {
        return toList(getJSONArray(object, name));
    }

    public static Map<String, Object> getMap(JSONObject object, String name) {
        return toMap(getJSONObject(object, name));
    }

    public static List<Map<String, Object>> toList(JSONArray array) {
        if (array == null) {
            return new ArrayList<>();
        }
        int length = array.length();
        List<Map<String, Object>> list = new ArrayList<>();
//        try {
        for (int i = 0; i < length; i++) {
//                Map<String, Object> map = new HashMap<>();
            JSONObject object = array.optJSONObject(i);
            if (object == null) {
                continue;
            }
//                JSONArray names = object.names();
//                int nl = names.length();
//                for (int j = 0; j < nl; j++) {
//                    map.put(names.optString(j), object.get(names.optString(j)));
//                }
            list.add(toMap(object));
        }
//        } catch (JSONException e) {
//            e.printStackTrace();
//        }
        return list;
    }

    public static Map<String, Object> toMap(JSONObject jsonObject) {
        Map<String, Object> map = new HashMap<>();
        try {
            if (jsonObject == null) {
                return map;
            }
            JSONArray names = jsonObject.names();
            int nl = names.length();
            for (int j = 0; j < nl; j++) {
                map.put(names.optString(j), jsonObject.get(names.optString(j)));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    public static JSONObject getJsonObject(JSONObject data, String key, Context context) throws JSONException {
        JSONArray dataArray = data.optJSONArray("data");
        if (dataArray== null||dataArray.length()==0) {
            Toast.makeText(context,"查询信息为空",Toast.LENGTH_SHORT).show();
            return null;
        }
        JSONObject jsonObject2 = new JSONObject(dataArray.get(0).toString());
        JSONArray facardinfo = jsonObject2.optJSONArray(key);
        if (facardinfo == null || facardinfo.length() == 0) {
            Toast.makeText(context,"查询信息为空",Toast.LENGTH_SHORT).show();
            return null;
        }

        return new JSONObject(facardinfo.get(0).toString());
    }

    public static JSONArray getJsonObjectArray(JSONObject data, String key, Context context) throws JSONException {
        JSONArray dataArray = data.optJSONArray("data");
        if (dataArray== null||dataArray.length()==0) {
            Toast.makeText(context,"查询信息为空",Toast.LENGTH_SHORT).show();
            return null;
        }
        JSONObject jsonObject2 = new JSONObject(dataArray.get(0).toString());
        if (jsonObject2 == null ) {
            Toast.makeText(context,"查询信息为空",Toast.LENGTH_SHORT).show();
            return null;
        }
        return jsonObject2.optJSONArray(key);
    }

    public static JSONArray toJsonObjectArray(JSONObject data, String key, Context context) throws JSONException {
        JSONArray dataArray = data.optJSONObject("data").getJSONArray(key);
        if (dataArray== null||dataArray.length()==0) {
            //Toast.makeText(context,"查询信息为空",Toast.LENGTH_SHORT).show();
            return null;
        }
       return dataArray;
    }

}
